﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.IO;

namespace ExplodingCow.Basilisk.Logic
{
    /// <summary>
    /// Data type for Saving a list of items in a current session to an xml file in order to be loaded later
    /// </summary>
    public class ListSaver
    {
        /// <summary>
        /// the only instance of this singleton class
        /// </summary>
        private static ListSaver instance;

        /// <summary>
        /// private singleton constructor
        /// </summary>
        private ListSaver()
        { 
        
        }
        /// <summary>
        /// Get's the current instance of ListSaver
        /// </summary>
        /// <returns>the only singleton instance of this class</returns>
        public static ListSaver GetListSaver()
        { 
            if (instance == null)
            {
                instance = new ListSaver();
            }
            return instance;
        }
        /// <summary>
        /// Save the List of Items to a specified directory and filename
        /// </summary>
        /// <param name="xmlSaveFilePath">the directory where to save the file</param>
        /// <param name="xmlSaveFile">the name of the file to save in the directory</param>
        /// <param name="itemsToSave">the list of items to save</param>
        public void Save(string xmlSaveFilePath, string xmlSaveFile, List<Item> itemsToSave)
        {
            //List<XElement> itemXElementData = new List<XElement>();
            //make all the private method calls within here to simplifify the algorithm structure
            XElement itemXElementData = createXML(itemsToSave);
            writeItemData(itemXElementData, xmlSaveFilePath, xmlSaveFile);
        }
        /// <summary>
        /// create xml data from a list of Items
        /// </summary>
        /// <param name="itemsToSave">the list of items to get data from</param>
        /// <returns>The XElement containing the items data</returns>
        private XElement createXML(List<Item> itemsToSave)
        {
            //implement all logic for creating the xml data, you can change the return types if you need to
            //I would say avoiding adding anymore public methods, bc there isn't much else for this class to do
            //add fields and properties as you see fit, but keep in mind this classes concerns. Good Luck :)

            // Pull items from the list and place them in an xDoc
            XElement itemList = new XElement("list");
            XElement itemsElement = new XElement("items");
            XElement itemElement;
            XElement itemNameElement;
            XElement itemCostElement;
            XElement itemQuantityElement;
            XElement itemCategoryElement;
            XElement itemDescriptionElement;
            XElement itemLocationElement;
            XElement itemLocationNameElement;
            XElement itemLocationAddressElement;
            XElement itemLocationCityElement;
            XElement itemLocationStateElement;
            XElement itemLocationZipElement;

            itemList.Add(itemsElement);
            foreach (Item item in itemsToSave)
            {
                itemElement = new XElement("item");
                itemNameElement = new XElement("name", item.Name);
                itemCostElement = new XElement("cost", item.Cost);
                itemQuantityElement = new XElement("quantity", item.Quantity);
                itemDescriptionElement = new XElement("description", item.Description);
                itemCategoryElement = new XElement("category", item.Category.ToString());
                itemLocationElement = new XElement("location");
                itemLocationNameElement = new XElement("name", item.StoreLocation.Name);
                itemLocationAddressElement = new XElement("address", item.StoreLocation.Address);
                itemLocationCityElement = new XElement("city", item.StoreLocation.City);
                itemLocationStateElement = new XElement("state", item.StoreLocation.State);
                itemLocationZipElement = new XElement("postal", item.StoreLocation.Zip);

                //append the attribute data
                itemElement.Add(itemNameElement);
                itemElement.Add(itemCostElement);
                itemElement.Add(itemQuantityElement);
                itemElement.Add(itemDescriptionElement);
                itemElement.Add(itemCategoryElement);
                itemElement.Add(itemLocationElement);

                //append the attribute data for the location member of Item
                itemLocationElement.Add(itemLocationNameElement);
                itemLocationElement.Add(itemLocationAddressElement);
                itemLocationElement.Add(itemLocationCityElement);
                itemLocationElement.Add(itemLocationStateElement);
                itemLocationElement.Add(itemLocationZipElement);

                //add everything to the items element
                itemsElement.Add(itemElement);
            }
            return itemList;
        }
        /// <summary>
        /// creates the save file in specified directory
        /// </summary>
        /// <param name="itemData">items data</param>
        /// <param name="directory">directory to save (not including filename)</param>
        /// <param name="fileName">the file name to save the xml file as</param>
        private void writeItemData(XElement itemData, string directory, string fileName)
        {
            string filePath = directory;
            itemData.Save(filePath);
        }


    }
}
